home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / ui / layout / example / appletbutton.java < prev    next >
Encoding:
Java Source  |  1996-02-26  |  5.2 KB  |  170 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. //TO DO: Close all windows when you leave the page?
  18.  
  19. import java.awt.*;
  20. import java.util.*;
  21. import java.applet.Applet;
  22.  
  23. public class AppletButton extends Applet implements Runnable {
  24.     int frameNumber = 1;
  25.     String windowClass;
  26.     String buttonText;
  27.     String windowText;
  28.     int requestedWidth = 0;
  29.     int requestedHeight = 0;
  30.     Button button;
  31.     Thread windowThread;
  32.     Label label;
  33.     boolean pleaseCreate = false;
  34.  
  35.     public void init() {
  36.     windowClass = getParameter("WINDOWTYPE");
  37.     if (windowClass == null) {
  38.         windowClass = "TestWindow";
  39.     }
  40.  
  41.     buttonText = getParameter("BUTTONTEXT");
  42.     if (buttonText == null) {
  43.         buttonText = "Click here to bring up a " + windowClass;
  44.     }
  45.  
  46.     windowText = getParameter("WINDOWTEXT");
  47.     if (windowText == null) {
  48.         windowText = windowClass;
  49.     }
  50.  
  51.         String windowWidthString = getParameter("WINDOWWIDTH");
  52.         if (windowWidthString != null) {
  53.             try {
  54.                 requestedWidth = Integer.parseInt(windowWidthString);
  55.             } catch (NumberFormatException e) {
  56.                 requestedWidth = 0;
  57.             }
  58.         }
  59.  
  60.         String windowHeightString = getParameter("WINDOWHEIGHT");
  61.         if (windowHeightString != null) {
  62.             try {
  63.                 requestedHeight = Integer.parseInt(windowHeightString);
  64.             } catch (NumberFormatException e) {
  65.                 requestedHeight = 0;
  66.             }
  67.         }
  68.  
  69.     setLayout(new GridLayout(2,0));
  70.     add(button = new Button(buttonText));
  71.         button.setFont(new Font("Helvetica", Font.PLAIN, 14));
  72.  
  73.     add(label = new Label("", Label.CENTER));
  74.     }
  75.  
  76.     public void start() {
  77.     if (windowThread == null) {
  78.         windowThread = new Thread(this, "Bringing Up " + windowClass);
  79.         windowThread.start();
  80.     }
  81.     }
  82.  
  83.     public synchronized void run() {
  84.     Class windowClassObject = null;
  85.     Class tmp = null;
  86.     String name = null;
  87.     
  88.     // Make sure the window class exists and is really a Frame.
  89.     // This has the added benefit of pre-loading the class,
  90.     // which makes it much quicker for the first window to come up.
  91.     try {
  92.         windowClassObject = Class.forName(windowClass);
  93.     } catch (Exception e) {
  94.         // The specified class isn't anywhere that we can find.
  95.         label.setText("Can't create window: Couldn't find class "
  96.                   + windowClass);
  97.         button.disable();
  98.     }
  99.  
  100.     // Find out whether the class is a Frame.
  101.     for (tmp = windowClassObject, name = tmp.getName();
  102.          !( name.equals("java.lang.Object") ||
  103.             name.equals("java.awt.Frame") ); ) {
  104.         tmp = tmp.getSuperclass();
  105.         name = tmp.getName();
  106.     }
  107.     if ((name == null) || name.equals("java.lang.Object")) {
  108.         //We can't run; ERROR; print status, never bring up window
  109.         label.setText("Can't create window: "
  110.                   + windowClass +
  111.               " isn't a Frame subclass.");
  112.         button.disable();
  113.     } else if (name.equals("java.awt.Frame")) { 
  114.         //Everything's OK. Wait until we're asked to create a window.
  115.         while (windowThread != null) {
  116.             while (pleaseCreate == false) {
  117.             try {
  118.                 wait();
  119.             } catch (InterruptedException e) {
  120.             }
  121.             }
  122.  
  123.             //We've been asked to bring up a window.
  124.             pleaseCreate = false;
  125.             Frame window = null;
  126.             try {
  127.                 window = (Frame)windowClassObject.newInstance();
  128.             } catch (Exception e) {
  129.             label.setText("Couldn't create instance of class "
  130.                       + windowClass);
  131.             }
  132.         if (frameNumber == 1) {
  133.                 window.setTitle(windowText);
  134.         } else {
  135.                 window.setTitle(windowText + ": " + frameNumber);
  136.         }
  137.             frameNumber++;
  138.  
  139.         //Set the window's size.
  140.             window.pack();
  141.                 if ((requestedWidth > 0) | (requestedHeight > 0)) {
  142.                     window.resize(Math.max(requestedWidth,
  143.                        window.size().width),
  144.                   Math.max(requestedHeight,
  145.                        window.size().height));
  146.                 }
  147.  
  148.             window.show();
  149.             label.setText("");
  150.         }
  151.     }
  152.     }
  153.         
  154.     public synchronized boolean action(Event event, Object what) {
  155.     if (event.target instanceof Button) {
  156.         //signal the window thread to build a window
  157.         label.setText("Please wait while the window comes up...");
  158.         pleaseCreate = true;
  159.         notify();
  160.     } 
  161.     return false;
  162.     }
  163. }
  164.  
  165. class TestWindow extends Frame {
  166.     public TestWindow() {
  167.     resize(300, 300);
  168.     }
  169. }
  170.